﻿using Acme.BookStore.RoomTable;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Volo.Abp.Application.Services;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using HIS;
using Microsoft.AspNetCore.Mvc;

namespace Acme.BookStore.GetRoomInfo
{
    /// <summary>
    /// 获取房型信息服务
    /// </summary>
    [ApiExplorerSettings(GroupName = "v1")]
    public class RoomInfoServices: ApplicationService,RoomInfoIServices
    {
        /// <summary>
        /// 仓储房型
        /// </summary>
        private readonly IRepository<RoomType> RoomTyperepository;
        /// <summary>
        /// 仓储房价
        /// </summary>
        private readonly IRepository<RoomPrices> RoomPricesRepository;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="roomTyperepository">仓储房型</param>
        /// <param name="roomPricesRepository">仓储房价</param>
        public RoomInfoServices(IRepository<RoomType> roomTyperepository, IRepository<RoomPrices> roomPricesRepository)
        {
            RoomTyperepository = roomTyperepository;
            RoomPricesRepository = roomPricesRepository;
        }
        /// <summary>
        /// 获取全部房型信息
        /// </summary>
        /// <param name="RoomTypeName">房型名称</param>
        /// <returns>返回任务</returns>
        [HttpGet("api/GetRoomInfo")]
        public async Task<APIResult<List<RoomInfoDTO>>> GetRoomInfo(string? RoomInfoName)
        {
            APIResult<List<RoomInfoDTO>> result = new APIResult<List<RoomInfoDTO>>();
            result.Code=CodeEnum.success;
            result.Message=CodeEnum.success.ToString();
            try
            {
                var list = from a in await RoomTyperepository.GetListAsync()
                           join b in await RoomPricesRepository.GetListAsync() on a.Id equals b.RoomTypeId
                           select new RoomInfoDTO
                           {
                               RoomName = a.RoomName,
                               RoomPrice = a.RoomPrice,
                               RoomDeposit = a.RoomDeposit,
                               RoomAcreage = a.RoomAcreage,
                               PeopleNumber = a.PeopleNumber,
                               Partner = a.Partner,
                               AddBed = a.AddBed,
                               RoomTypeImage = a.RoomTypeImage,
                               RoomTypeVideo = a.RoomTypeVideo,
                               RoomTypeVR = a.RoomTypeVR,
                               RoomTypeDesc = a.RoomTypeDesc,
                               RoomTypeId = b.RoomTypeId,
                               ProductName = b.ProductName,
                               Price = b.Price,
                               PayoutMethod = b.PayoutMethod,
                               AccommodationOffers = b.AccommodationOffers,
                               MembershipSpread = b.MembershipSpread,
                               BreakfastNumber = b.BreakfastNumber,
                               RoomNumber = b.RoomNumber,
                               SalesStrategy = b.SalesStrategy,
                               RoomStatus = b.RoomStatus,
                               RoomSort = b.RoomSort,
                           };
                if (!string.IsNullOrEmpty(RoomInfoName))
                {
                    list=list.Where(x=>x.RoomName.Contains(RoomInfoName)).ToList();
                }
                result.Data=list.ToList();
            }
            catch (Exception e)
            {
                result.Code = CodeEnum.error;
                result.Message = CodeEnum.error.ToString()+e.Message;
                //throw;
            }
            return await Task.FromResult(result);
        }
        /// <summary>
        /// 获取当前房型信息
        /// </summary>
        /// <param name="Id">房型id</param>
        /// <returns>返回任务</returns>
        [HttpGet("api/GetCurrentRoomInfo")]
        public async Task<APIResult<RoomInfoDTO>> GetCurrentRoomInfo(int Id)
        {
            APIResult<RoomInfoDTO> result = new APIResult<RoomInfoDTO>();
            result.Code = CodeEnum.success;
            result.Message = CodeEnum.success.ToString();
            try
            {
                var list = from a in await RoomTyperepository.GetListAsync()
                           join b in await RoomPricesRepository.GetListAsync() on a.Id equals b.RoomTypeId
                           where a.Id == Id
                           select new RoomInfoDTO
                           {
                               RoomName = a.RoomName,
                               RoomPrice = a.RoomPrice,
                               RoomDeposit = a.RoomDeposit,
                               RoomAcreage = a.RoomAcreage,
                               PeopleNumber = a.PeopleNumber,
                               Partner = a.Partner,
                               AddBed = a.AddBed,
                               RoomTypeImage = a.RoomTypeImage,
                               RoomTypeVideo = a.RoomTypeVideo,
                               RoomTypeVR = a.RoomTypeVR,
                               RoomTypeDesc = a.RoomTypeDesc,
                               RoomTypeId = b.RoomTypeId,
                               ProductName = b.ProductName,
                               Price = b.Price,
                               PayoutMethod = b.PayoutMethod,
                               AccommodationOffers = b.AccommodationOffers,
                               MembershipSpread = b.MembershipSpread,
                               BreakfastNumber = b.BreakfastNumber,
                               RoomNumber = b.RoomNumber,
                               SalesStrategy = b.SalesStrategy,
                               RoomStatus = b.RoomStatus,
                               RoomSort = b.RoomSort,
                           };
                result.Data=list.FirstOrDefault();
            }
            catch (Exception e)
            {
                result.Code = CodeEnum.error;
                result.Message = CodeEnum.error.ToString() + e.Message;
                //throw;
            }
            return await Task.FromResult(result);
        }
    }
}
